package com.ruoyi.lm.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lm.domain.*;
import com.ruoyi.lm.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 需求单Controller
 * 
 * @author ruoyi
 * @date 2020-09-02
 */
@Controller
@RequestMapping("/phone/lm/require")
public class LmRequireControllerPhone extends BaseController
{

    @Autowired
    private ILmRequireService lmRequireService;
    @Autowired
    private ILmUserService lmUserService;
    @Autowired
    private ILmTimesrecordService lmTimesrecordService;
    @Autowired
    private ILmUsersignService lmUsersignService;
    /**
     * 查询需求单列表(降序)
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestBody LmRequire lmRequire)
    {
        List<LmRequire> list = lmRequireService.selectLmRequireList(lmRequire);
        return getDataTable(list);
    }

    /**
     * 查询需求单列表(升序)
     */
    @PostMapping("/listRe")
    @ResponseBody
    public TableDataInfo listRe(@RequestBody LmRequire lmRequire)
    {
        List<LmRequire> list = lmRequireService.selectLmRequireListRe(lmRequire);
        return getDataTable(list);
    }

    /**
     * 查询需求单总数
     */
    @PostMapping("/getRequireCount")
    @ResponseBody
    public AjaxResult getRequireCount()
    {
        return new AjaxResult(AjaxResult.Type.SUCCESS,"200",lmRequireService.getRequireCount());
    }

    /**
     * 查询服务需求活动报名者信息
     */
    @PostMapping("/listRequreByUserId")
    @ResponseBody
    public AjaxResult listRequreByUserId(String requireId,String userId,String buserId)
    {
        LmRequire lmRequire = lmRequireService.selectLmRequireById(requireId);
        LmUser lmUser = lmUserService.selectLmUserById(lmRequire.getUserId());
        //判断当前用户次数是否够用
        if(lmUser.getPackageTimes()>0){
            //减掉一次
            lmUser.setPackageTimes(lmUser.getPackageTimes()-1);
            lmUserService.updateLmUser(lmUser);
            //添加次数记录
            //0表示报名需求
            LmTimesrecord lmTimesrecord = new LmTimesrecord();
            lmTimesrecord.setId(IdWorker.getId());
            lmTimesrecord.setRecordType("1");//减少
            lmTimesrecord.setRecordUser(lmUser.getId());
            lmTimesrecord.setRequireId(requireId);
            lmTimesrecord.setTimes(1);
            lmTimesrecord.setUserId(buserId);
            lmTimesrecordService.insertLmTimesrecord(lmTimesrecord);
            //返回报名用户信息
            return new AjaxResult(AjaxResult.Type.SUCCESS,"200",lmUserService.selectLmUserById(userId));
        }else{
            return error("次数已用完，请重新购买!");
        }
    }

    /**
     * 获取需求电话信息
     */
    @PostMapping("/getRequirePhone")
    @ResponseBody
    public AjaxResult getRequirePhone(String requireId,String userId,String buserId)
    {
        LmRequire lmRequire = lmRequireService.selectLmRequireById(requireId);
        LmUser lmUser = lmUserService.selectLmUserById(userId);
        //判断当前用户次数是否够用
        if(lmUser.getPackageTimes()>0){
            //减掉一次
            lmUser.setPackageTimes(lmUser.getPackageTimes()-1);
            lmUserService.updateLmUser(lmUser);
            //添加次数记录
            LmTimesrecord lmTimesrecord = new LmTimesrecord();
            lmTimesrecord.setId(IdWorker.getId());
            lmTimesrecord.setRecordType("1");//减少
            lmTimesrecord.setRecordUser(lmUser.getId());
            lmTimesrecord.setRequireId(requireId);
            lmTimesrecord.setTimes(1);
            lmTimesrecord.setUserId(buserId);
            lmTimesrecordService.insertLmTimesrecord(lmTimesrecord);

            LmRequire lmRequire1 = new LmRequire();
            lmRequire1.setId(lmRequire.getId());
            String clickPhone = String.valueOf(lmRequire.getClickPhoneNum());
            if(EmptyUtils.isNotEmpty(clickPhone)){
                lmRequire1.setClickPhoneNum(lmRequire.getClickPhoneNum() + 1);
            }else{
                lmRequire1.setClickPhoneNum(0);
            }
            lmRequireService.updateLmRequire(lmRequire1);

            //返回需求信息
            return new AjaxResult(AjaxResult.Type.SUCCESS,"200", lmRequire);
        }else{
            return error("次数已用完，请重新购买!");
        }
    }


    /**
     * 查询需求单详情
     */
    @PostMapping("/listById")
    @ResponseBody
    public AjaxResult listById(String id)
    {
        List<Map> resultList = new ArrayList<>();
        //添加服务详情
        Map temp = new HashMap();
        temp.put("detail",lmRequireService.selectLmRequireById(id));
        //获取服务报名人数
        LmUsersign lmUsersign = new LmUsersign();
        lmUsersign.setSignId(id);
        List<LmUsersign> childList = lmUsersignService.selectLmUsersignList(lmUsersign);
        temp.put("sign",childList.size());
        resultList.add(temp);
        return new AjaxResult(AjaxResult.Type.SUCCESS,"200",resultList);
    }

    /**
     * 增加点击量
     */
    @PostMapping("/addClickNum")
    @ResponseBody
    public AjaxResult addClickNum(String id)
    {
        LmRequire lmRequire1 = lmRequireService.selectLmRequireById(id);
        LmRequire lmRequire = new LmRequire();
        lmRequire.setId(id);
        lmRequire.setClickNum(lmRequire1.getClickNum()+1);
        return new AjaxResult(AjaxResult.Type.SUCCESS,"200",lmRequireService.updateLmRequire(lmRequire));
    }

    /**
     * 新增保存需求单
     */
    @Log(title = "需求单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody LmRequire lmRequire)
    {
        //取详情图片
        String[] images = lmRequire.getRequireImages().split(",");
        lmRequire.setRequirePic(images[0]);
        lmRequire.setClickNum(0);
        lmRequire.setRequireStatus("上架");
        return toAjax(lmRequireService.insertLmRequire(lmRequire));
    }

    /**
     * 修改保存需求单
     */
    @Log(title = "需求单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody LmRequire lmRequire)
    {
        //取详情图片
        String[] images = lmRequire.getRequireImages().split(",");
        lmRequire.setRequirePic(images[0]);
        return toAjax(lmRequireService.updateLmRequire(lmRequire));
    }

    /**
     * 删除需求单
     */
    @Log(title = "需求单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(lmRequireService.deleteLmRequireByIds(ids));
    }
}
